package c.h.a.m0.s;

import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCaptureSession.StateCallback;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraManager.AvailabilityCallback;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureRequest.Builder;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.text.TextUtils;
import android.util.Size;
import android.view.Surface;
import java.util.ArrayList;
import java.util.Objects;

/* compiled from: TorchControl */
public class c extends a {
    public CameraDevice e;
    public SurfaceTexture f;
    public Surface g;
    public CaptureRequest h;
    public CaptureRequest i;
    public AvailabilityCallback j = new a();
    public CameraCaptureSession k;
    public final StateCallback l = new b();

    /* compiled from: TorchControl */
    public class a extends AvailabilityCallback {
        public void onCameraAvailable(String str) {
            if ((TextUtils.equals(str, c.this.b) ^ 1) == 0) {
                c.this.e(false);
            }
        }

        public void onCameraUnavailable(String str) {
            if ((TextUtils.equals(str, c.this.b) ^ 1) == 0) {
                c.this.e(true);
            }
        }
    }

    /* compiled from: TorchControl */
    public class b extends StateCallback {
        public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
            c.g(c.this);
        }

        public void onConfigured(CameraCaptureSession cameraCaptureSession) {
            c cVar = c.this;
            cVar.k = cameraCaptureSession;
            cVar.e(true);
            c.this.f(false);
        }
    }

    /* compiled from: TorchControl */
    public class c extends CameraDevice.StateCallback {
        public void onDisconnected(CameraDevice cameraDevice) {
            c cVar = c.this;
            if (cameraDevice == cVar.e) {
                c.g(cVar);
            }
        }

        public void onError(CameraDevice cameraDevice, int i) {
            c cVar = c.this;
            if (cameraDevice == cVar.e) {
                c.g(cVar);
            }
        }

        public void onOpened(CameraDevice cameraDevice) {
            c cVar = c.this;
            cVar.e = cameraDevice;
            if (cameraDevice == null) {
                c.g(cVar);
                return;
            }
            cVar.a.registerAvailabilityCallback(cVar.j, null);
            try {
                c.h(c.this);
            } catch (CameraAccessException unused) {
                c.g(c.this);
            }
        }
    }

    public c(CameraManager cameraManager) {
        this.a = cameraManager;
        String c = c(cameraManager);
        this.b = c;
        try {
            this.a.openCamera(c, new c(), null);
        } catch (CameraAccessException unused) {
            f(false);
        }
    }

    public static void g(c cVar) {
        cVar.release();
        cVar.f(false);
        cVar.e(false);
    }

    public static void h(c cVar) throws CameraAccessException {
        Size size;
        Objects.requireNonNull(cVar);
        int i = 0;
        cVar.f = new SurfaceTexture(0, false);
        StreamConfigurationMap streamConfigurationMap = (StreamConfigurationMap) cVar.a.getCameraCharacteristics(cVar.e.getId()).get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        if (streamConfigurationMap == null) {
            size = null;
        } else {
            Size[] outputSizes = streamConfigurationMap.getOutputSizes(SurfaceTexture.class);
            size = outputSizes[0];
            int length = outputSizes.length;
            while (i < length) {
                Size size2 = outputSizes[i];
                if (size.getWidth() >= size2.getWidth() && size.getHeight() >= size2.getHeight()) {
                    size = size2;
                }
                i++;
            }
        }
        if (size != null) {
            cVar.f.setDefaultBufferSize(size.getWidth(), size.getHeight());
            cVar.g = new Surface(cVar.f);
            ArrayList arrayList = new ArrayList(1);
            arrayList.add(cVar.g);
            cVar.e.createCaptureSession(arrayList, cVar.l, null);
            Builder createCaptureRequest = cVar.e.createCaptureRequest(1);
            createCaptureRequest.set(CaptureRequest.FLASH_MODE, Integer.valueOf(2));
            createCaptureRequest.addTarget(cVar.g);
            cVar.h = createCaptureRequest.build();
            createCaptureRequest = cVar.e.createCaptureRequest(1);
            createCaptureRequest.addTarget(cVar.g);
            cVar.i = createCaptureRequest.build();
            return;
        }
        throw new CameraAccessException(3, "Size is null");
    }

    public void a() {
        if (this.e != null && this.k != null && this.i != null && d()) {
            try {
                this.k.capture(this.i, null, null);
                f(false);
            } catch (Exception unused) {
            }
        }
    }

    public void b() {
        if (this.e != null && this.k != null && this.h != null && !d()) {
            try {
                this.k.capture(this.h, null, null);
                f(true);
            } catch (Exception unused) {
            }
        }
    }

    public void release() {
        CameraDevice cameraDevice = this.e;
        if (cameraDevice != null) {
            cameraDevice.close();
            this.e = null;
        }
        CameraManager cameraManager = this.a;
        if (cameraManager != null) {
            AvailabilityCallback availabilityCallback = this.j;
            if (availabilityCallback != null) {
                cameraManager.unregisterAvailabilityCallback(availabilityCallback);
                this.j = null;
                this.a = null;
            }
        }
    }
}
